package org.itzheng.itplayer.local

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import org.itzheng.base.BaseFragment
import org.itzheng.itplayer.R
import org.itzheng.itplayer.adapter.FolderListPreviewAdapter
import org.itzheng.itplayer.bean.VideoFolder
import org.itzheng.setGridAdapter
import kotlin.collections.ArrayList

/**
 * Title:文件夹列表<br></br>
 * Description: <br></br>
 * 文件夹名字，视频个数
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2020-12-4.
 */
class LocalVideoFolderFragment : BaseFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflate(R.layout.fragment_recycler_view)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setAdapter(getVideoFolders(arguments))
    }

    fun setAdapter(items: List<VideoFolder?>?) {
        val adapter = FolderListPreviewAdapter()
        adapter.setOnItemClickListener { view, position ->
            val folder = adapter.getItem(position) as VideoFolder
            startFragment(LocalVideoFileFragment.newInstance(folder.fContent))
        }
        rootView.findViewById<RecyclerView>(R.id.rcvContent).setGridAdapter(adapter, 2)
        adapter.submitList(items)

    }

    companion object {
        private const val KEY_LIST_VIDEO_FOLDER = "KEY_LIST_VIDEO_FOLDER"
        fun getVideoFolders(arguments: Bundle?): List<VideoFolder?>? {
            val obj = arguments!!.getSerializable(KEY_LIST_VIDEO_FOLDER) ?: return null
            return obj as List<VideoFolder?>
        }

        fun newInstance(items: ArrayList<VideoFolder>): LocalVideoFolderFragment {
            val args = Bundle()
            args.putSerializable(KEY_LIST_VIDEO_FOLDER, items)
            val fragment = LocalVideoFolderFragment()
            fragment.arguments = args
            return fragment
        }
    }
}